<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.inc.php';

$template["title"] = "发布帖子";

$link = connect();
if(!$member_id = is_login($link)){
    skip("login.php", 'error', '请登录再发帖！');
}

if(isset($_POST['submit'])){
    include_once 'inc/check_publish.inc.php';

    $_POST = escape($link, $_POST);

    $query = "insert into kuse_content(module_id, title, content, time, member_id) values({$_POST['module_id']}, '{$_POST['title']}', '{$_POST['content']}', now(), {$member_id})";
    execute($link, $query);
    if(mysqli_affected_rows($link) ==1){
        skip("publish.php", 'ok', "恭喜您，发帖成功！");
    }else{
        skip("publish.php", 'error', "对不起，发帖失败，请重试！");
    }
}
?>

<?php include_once 'inc/header.inc.php';?>
<!-- 主体代码开始 -->
    <!-- 顶层版块位置 -->
    <div id="position" class="content-box">
        <a href="">首页</a>
        >
        <a href="">发布帖子</a>
    </div>

    <!-- 发帖版块 -->
    <div id="publish" class="content-box">
        <!-- 选择帖子版块 -->
        <form method="post">
            <select name="module_id">
                <option>请选择一个版块</option>
                <?php
                $query = "select * from kuse_father_module order by sort desc";
                $result_father = execute($link, $query);
                while($data_father=mysqli_fetch_assoc($result_father)){
                    echo "<optgroup label = '{$data_father['module_name']}'>";
                    $query = "select * from kuse_son_module where father_module_id = {$data_father['id']} order by sort desc";
                    $result_son = execute($link, $query);
                    while($data_son = mysqli_fetch_assoc($result_son)){
                        echo "<option value='{$data_son['id']}'>{$data_son['module_name']}</option>";
                    }
                    echo "</optgroup>";
                }
                ?>
                
            </select>
            <input class="title" name="title" type="text" placeholder="请输入标题">
            <textarea class="content" name="content"></textarea>
            <input class="publish" type="submit" name="submit" value="">
        </form>
    </div>
<!-- 主体代码结束 -->
<?php include_once 'inc/footer.inc.php'; ?>